<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    
    ES6 for in与for of - DSRBLOG
    
  </title>
  

  
  
  <link href="atom.xml" rel="alternate" title="DSRBLOG" type="application/atom+xml">
  <link rel="stylesheet" href="asset/css/style.min.css">
  <link rel="stylesheet" href="asset/css/doc.css">
  <script src="asset/app.js"></script>


</head>

<body> <section class="ct-body">
	<div class="container">
		<div class="columns is-variable bd-klmn-columns is-4 is-centered">
			<div class="column is-four-fifths">
				<div class="post-body single-content">
					
					<h1 class="title">
						ES6 for in与for of
					</h1>


					<div class="media">
						
						
						<div class="media-content">
							<div class="content">
								<p>
									<span class="date">2022/08/20 15:34 下午</span>
									

									

									<br />
									<span class="tran-tags">Tags:</span>&nbsp;
									
									<a class="tag is-link is-light">#ES6新特性</a>
									

								</p>
							</div>
						</div>

					</div>
				</div>
				<article class="markdown-body single-content">
					<p><code>for in</code>是<code>ES5</code>的语法标准，而<code>for of</code>是<code>ES6</code>的语法标准</p>
<pre class="line-numbers"><code class="language-js">const arr = ['a', 'b', 'c'];

//for in
for(let i in arr) {
    console.log(i); //输出 '0', '1', '2'
}

//for of
for(let i of arr) {
    console.log(i); //输出 'a', 'b', 'c'
}
</code></pre>
<p>通过<code>for in</code>遍历得到下标，而通过<code>for of</code>遍历得到属性值，并且<code>for in</code>遍历得到的下标是<code>String</code>类型，不是<code>Number</code>类型</p>
<ul>
<li><code>for...in</code>可以遍历原型链上的属性，<code>for...of</code>不行</li>
</ul>

				</article>
				<div class="comments-wrap">
					<div class="share-comments">
						

						

						
					</div>
				</div><!-- end comments wrap -->
			</div>
		</div><!-- end columns -->
	</div><!-- end container -->
</section>  














<script src="asset/prism.js"></script>









</body>

</html>